package com.example.listener;

import com.example.common.result.Result;
import com.example.common.util.SpringContextUtil;
import com.example.controller.ZnxController;
import com.example.entity.dto.TaskInit;
import com.example.entity.hz.Znx;
import com.example.exception.BaseException;
import com.example.mapper.TaskInitMapper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理任务创建时根据自定义的审批用户指定候选人
 */
@Component
public class TaskInitListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        TaskInitMapper taskInitMapper = SpringContextUtil.getBean(TaskInitMapper.class);
        RuntimeService runtimeService = SpringContextUtil.getBean(RuntimeService.class);
        ZnxController znxController=SpringContextUtil.getBean(ZnxController.class);
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(delegateTask.getProcessInstanceId()).singleResult();
        String[] split = processInstance.getBusinessKey().split(":");
        String selectFormKey = split[0];
        String id=split[1]+":"+split[2];
        TaskInit taskInit = taskInitMapper.selectAllByFormKeyAndTaskId(selectFormKey, delegateTask.getTaskDefinitionKey()).get(0);
        List<Znx> znxList=new ArrayList<>();
        if (!(taskInit.getAssignee().isEmpty())){
            delegateTask.setAssignee(null);
            String assignee = taskInit.getAssignee();
            switch (assignee) {
                case "applicate":
                    String initiator = runtimeService.getVariable(delegateTask.getExecutionId(), "initiator", String.class);
                    delegateTask.addCandidateUser(initiator);
                    Znx znx=Znx.builder().code(id).content("您有一条新的单据待审核")
                            .notificationCode(initiator).processId(delegateTask.getProcessInstanceId()).title("待办").build();
                    znxList.add(znx);
                    break;
                case "leader":
                    String leader = runtimeService.getVariable(delegateTask.getExecutionId(), "leader", String.class);
                    if (leader!=null&& !leader.isEmpty()){
                        delegateTask.addCandidateUser(leader);
                        Znx znx2=Znx.builder().code(id).content("您有一条新的单据待审核")
                                .notificationCode(leader).processId(delegateTask.getProcessInstanceId()).title("待办").build();
                        znxList.add(znx2);
                    }
                    break;
                default:
                    delegateTask.addCandidateUser(assignee);
                    Znx znx3=Znx.builder().code(id).content("您有一条新的单据待审核")
                            .notificationCode(assignee).processId(delegateTask.getProcessInstanceId()).title("待办").build();
                    znxList.add(znx3);
            }
        }
        if (!taskInit.getCandidateUsers().isEmpty()) {
            delegateTask.setAssignee(null);
            List<String> candidateUsers = taskInit.getCandidateUsers();
            candidateUsers.forEach(candidateUser->{
                switch (candidateUser) {
                    case "applicate":
                        String initiator = runtimeService.getVariable(delegateTask.getExecutionId(), "initiator", String.class);
                        delegateTask.addCandidateUser(initiator);
                        Znx znx=Znx.builder().code(id).content("您有一条新的单据待审核")
                                .notificationCode(initiator).processId(delegateTask.getProcessInstanceId()).title("待办").build();
                        znxList.add(znx);
                        break;
                    case "leader":
                        String leader = runtimeService.getVariable(delegateTask.getExecutionId(), "leader", String.class);
                        if (leader!=null&& !leader.isEmpty()){
                            delegateTask.addCandidateUser(leader);
                            Znx znx2=Znx.builder().code(id).content("您有一条新的单据待审核")
                                    .notificationCode(leader).processId(delegateTask.getProcessInstanceId()).title("待办").build();
                            znxList.add(znx2);
                        }
                        break;
                    default:
                        delegateTask.addCandidateUser(candidateUser);
                        Znx znx3=Znx.builder().code(id).content("您有一条新的单据待审核")
                                .notificationCode(candidateUser).processId(delegateTask.getProcessInstanceId()).title("待办").build();
                        znxList.add(znx3);
                }
            });
        }

        TaskInit taskInits = taskInitMapper.selectAllByFormKeyAndTaskIds(selectFormKey, delegateTask.getTaskDefinitionKey()).get(0);
        if (!taskInits.getCandidateUsers().isEmpty()) {
            delegateTask.setAssignee(null);
            List<String> candidateUsers = taskInits.getCandidateUsers();
            candidateUsers.forEach(candidateUser->{
                Znx znx3=Znx.builder().code(id).content("您有一条新的单据查看")
                    .notificationCode(candidateUser).processId(delegateTask.getProcessInstanceId()).title("待办").build();znxList.add(znx3);
            });
        }

        Result result = znxController.insertZnxS(znxList);
        if (!result.getCode().equals("200")) {
            throw new BaseException("站内信传递失败！");
        }
    }
}
